import { observable, action } from 'mobx';
import debounce from 'lodash/debounce';
import { message } from 'antd';
import { ListStore } from './listStore'
import { WeaTableNew } from 'comsMobx';
import * as API_Journal from '../apis/journal';
const { TableStore } = WeaTableNew;
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
/**
 * 刊物编辑Store
 */
export default class JournalEditStore extends ListStore {

    @observable kwid = '0';

    @observable lmid = '0';

    @observable tableStore1 = new TableStore();

    @observable state = '0';

    @observable visible = false;

    @observable visible1 = false;

    @observable visible2 = false;

    @observable visible3 = false;

    @observable defColumns = [];

    @observable columnDatas = [];

    @observable defColumns1 = [];

    @observable columnDatas1 = [];

    @observable options = [];

    @observable gjid = '';

    @observable tjlx = '';

    @observable fromdate = '';

    @observable todate = '';

    @observable title = '';

    @observable xxlb = '';

    @observable buttonVisible = false;

    @observable url = '';

    @observable buttonVisible1 = false;

    initList = (params) =>{
        this.kwid = params.kwid;
        API_Journal.getContributionlList({...params,type:'lanmu'}).then(data => {
            this.tableStore = new TableStore({
                dataHandle: (datas) => {  //任务列表
                    datas.forEach((data) => {
                        if (typeof (data.subListspan) == 'string') {
                            data.subListspan = data.subListspan ? JSON.parse(data.subListspan) : undefined;
                        }
                    });
                    return datas;
                }
            });
            this.tableStore.getDatas(data.sessionkey);
        })
        let type = 'contribution';
        if(this.state=='3'){
            type = 'leaderComment';
        }
        API_Journal.getContributionlList({...params,type:type,state:this.state}).then(data => {
            this.tableStore1 = new TableStore();
            this.tableStore1.getDatas(data.sessionkey);
            if(this.url==''){
                this.url = data.url;
            }
        })
    }

    reserve = () =>{
        API_Journal.contributionOperation({ids:this.tableStore1.selectedRowKeysAllPages,type:'reserve'}).then(data => {
            this.initList({kwid:this.kwid});
        })
    }

    discard = () =>{
        API_Journal.contributionOperation({ids:this.tableStore1.selectedRowKeysAllPages,type:'discard'}).then(data => {
            this.initList({kwid:this.kwid});
        })
    }

    deleteReserve = () =>{
        API_Journal.contributionOperation({ids:this.tableStore1.selectedRowKeysAllPages,type:'deleteReserve'}).then(data => {
            this.initList({kwid:this.kwid});
        })
    }

    recover = () =>{
        API_Journal.contributionOperation({ids:this.tableStore1.selectedRowKeysAllPages,type:'recover'}).then(data => {
            this.initList({kwid:this.kwid});
        })
    }

    adopt = debounce(() =>{
        this.buttonVisible = true;
        this.buttonVisible1 = true;
        const params = {lmid:this.lmid,kwid:this.kwid,ids:this.tableStore1.selectedRowKeysAllPages,type:'adopt',state:this.state};
        API_Journal.contributionOperation(params).then(data => {
            this.visible1 = false;
            this.buttonVisible = false;
            this.buttonVisible1 = false;
            this.initList({kwid:this.kwid});
            window.opener._table.reLoad(); 
        });
    }, 300)

    deleteAdopt = (params) =>{
        API_Journal.contributionOperation({...params,type:'deleteAdopt',kwid:this.kwid}).then(data => {
            this.initList({kwid:this.kwid});
        });
    }

    reserve1 = (params) =>{
        API_Journal.contributionOperation({...params,type:'reserve1',kwid:this.kwid}).then(data => {
            this.initList({kwid:this.kwid});
        })
    }

    changeLanmu = () =>{
        API_Journal.contributionOperation({gjid:this.gjid,type:'changeLanmu',kwid:this.kwid,lmid:this.lmid}).then(data => {
            if(data.success){
                this.initList({kwid:this.kwid});
                this.visible2 = false;
            }
        })
    }

    submitDatas1 = () =>{
        console.log(this.columnDatas1)
        const params = {datas:JSON.stringify(this.columnDatas1),type:'contribution',kwid:this.kwid};
        API_Journal.submitDatas(params).then(data => {
            if(data.success){
                this.visible3 = false;
                message.success('排序成功！');
                this.initList({kwid:this.kwid});
            }else{
                message.success('排序失败！');
            }
        })
    }

    getDiscardInfo = () =>{
        this.state = '2';
        const params = {kwid:this.kwid,state:this.state};
        API_Journal.getContributionlList({...params,type:'contribution'}).then(data => {
            this.tableStore1 = new TableStore();
            this.tableStore1.getDatas(data.sessionkey);
        })
    }

    getReserveInfo = () =>{
        this.state = '1';
        const params = {kwid:this.kwid,state:this.state};
        API_Journal.getContributionlList({...params,type:'contribution'}).then(data => {
            this.tableStore1 = new TableStore();
            this.tableStore1.getDatas(data.sessionkey);
        })
    }

    getDraftInfo = () =>{
        this.state = '0';
        const params = {kwid:this.kwid,state:this.state};
        API_Journal.getContributionlList({...params,type:'contribution'}).then(data => {
            this.tableStore1 = new TableStore();
            this.tableStore1.getDatas(data.sessionkey);
        })
    }

    setVisible = (visible) =>{
        this.visible = visible;
        const params = {kwid:this.kwid};
        if(visible){
            API_Journal.getLanmuList(params).then(data => {
                this.defColumns = data.columns;
                this.columnDatas = data.columnDatas;
            })
        }
    }

    setVisible1 = (flag) =>{
        this.visible1 = flag;
        if(this.visible1){
            API_Journal.getLanmuList({type:'select',kwid:this.kwid}).then(data => {
                this.lmid = data.lmid;
                this.options = data.options;
            })
        } 
    }

    setVisible2 = (flag) =>{
        this.visible2 = flag;
        if(this.visible2){
            API_Journal.getLanmuList({type:'select',kwid:this.kwid}).then(data => {
                this.lmid = data.lmid;
                this.options = data.options;
            })
        } 
    }

    setVisible3 = (flag) =>{
        this.visible3 = flag;
        if(this.visible3){
            API_Journal.getLanmuList({type:'contribution',kwid:this.kwid,lmid:this.lmid}).then(data => {
                this.defColumns1 = data.columns;
                this.columnDatas1 = data.columnDatas;
            })
        } 
    }

    setColumnDatas = (datas) => {
        let _this = this;
        _this.columnDatas = datas;
    }

    setColumnDatas1 = (datas) => {
        let _this = this;
        _this.columnDatas1 = datas;
    }

    setLmid = (v) =>{
        this.lmid = v;
    }

    setGjid = (v) =>{
        this.gjid = v;
    }

    setTjlx = (v) =>{
        this.tjlx = v;
    }

    setFromdate = (v) =>{
        this.fromdate = v;
    }

    setTodate = (v) =>{
        this.todate = v;
    }

    setTitle = (v) =>{
        this.title = v;
    }

    setXxlb = (v) =>{
        this.xxlb = v;
    }

    submitDatas = () =>{
        const params = {kwid:this.kwid,datas:JSON.stringify(this.columnDatas)};
        API_Journal.submitDatas(params).then(data => {
            if(data.success){
                this.visible = false;
                message.success('保存成功！');
                this.initList({kwid:this.kwid});
            }else{
                message.success('保存失败！');
            }
        })
    }

    doSearch = () =>{
        let type = 'contribution';
        if(this.state=='3'){
            type = 'leaderComment';
        }
        const params = {kwid:this.kwid,tjlx:this.tjlx,fromdate:this.fromdate,todate:this.todate,title:this.title,xxlb:this.xxlb};
        API_Journal.getContributionlList({...params,type:type,state:this.state}).then(data => {
            this.tableStore1 = new TableStore();
            this.tableStore1.getDatas(data.sessionkey);
        })
    }

    createProof = () =>{
        window.open("/spa/info/static/index.html#/main/info/infoWordMerge?journalid="+this.kwid, "_blank");
    }

    createInfo = () =>{
        window.open(this.url, "_blank");
    }

    leaderComment = () =>{
        this.state = '3';
        const params = {kwid:this.kwid};
        API_Journal.getContributionlList({...params,type:'leaderComment'}).then(data => {
            this.tableStore1 = new TableStore();
            this.tableStore1.getDatas(data.sessionkey);
        })
    }

    //刷新列表
    @action
    reLoad = () => {
        this.tableStore.getDatas(this.tableStore.dataKey, this.tableStore.current, this.tableStore.pageSize, this.tableStore.sortParams);
        this.tableStore1.getDatas(this.tableStore1.dataKey, this.tableStore1.current, this.tableStore1.pageSize, this.tableStore1.sortParams);
    }

}
